package cn.element.netty.future;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class TestNettyPromise {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1.准备EventLoop对象
        EventLoop group = new NioEventLoopGroup().next();
         
        // 2.可以主动创建Promise, 结果容器
        DefaultPromise<Integer> promise = new DefaultPromise<>(group);
        
        new Thread(() -> {
            // 3.任意一个线程执行计算,计算完成之后向Promise填充结果
            log.debug("开始计算...");
            try {
                Thread.sleep(1000);
                promise.setSuccess(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
                promise.setFailure(e);
            }
        }).start();
        
        // 4.接收结果的线程
        log.debug("等待结果...");
        log.debug("结果是: {}", promise.get());
    }

}
